//
//  String+Extension.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/29.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import Foundation

extension String {
    func sourceURLAndText() -> (text : String?, urlStr : String?) {
        
        let pattern = "<a href=\"(.*?)\" .*?>(.*?)</a>"
        
        let regEx = try! NSRegularExpression(pattern: pattern, options: []) //加try! 后 如果失败会报错,方便调试
        
        let result = regEx.firstMatchInString(self, options: [], range: NSRange(location: 0, length: characters.count))
        
//        print(result)
        /*
        po result?.rangeAtIndex(0)
        ▿ Optional((0,74))
        ▿ Some : _NSRange
        - location : 0
        - length : 74  所有的
        
        po result?.rangeAtIndex(1)
        ▿ Optional((9,34))
        ▿ Some : _NSRange
        - location : 9
        - length : 34 地址url
        
        po result?.rangeAtIndex(2)
        ▿ Optional((60,10))
        ▿ Some : _NSRange
        - location : 60
        - length : 10 来源字符串
        */
        let rangeURL = result?.rangeAtIndex(1)
        let rangeText = result?.rangeAtIndex(2)
        
        var subStrURL = ""
        var subStrText = ""
        
        if let rurl = rangeURL {
        subStrURL = (self as NSString).substringWithRange(rurl)
        }
        if let rtext = rangeText {
        subStrText = (self as NSString).substringWithRange(rtext)
        }
        return (subStrText,subStrURL)
        
    }
}